// crawl-book.js
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
const TurndownService = require('turndown');
const { sleep, randomMs } = require('./common/util');

const baseSite = '知源中医';
const outputDir = './docs/' + baseSite;
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });

const turndownService = new TurndownService({
  headingStyle: 'atx',
  codeBlockStyle: 'fenced',
  emDelimiter: '*',
});

turndownService.addRule('images', {
  filter: ['img'],
  replacement: function (content, node) {
    const src = node.getAttribute('src');
    const alt = node.getAttribute('alt') || '';
    return `[${alt}](${src})`;
  }
});

(async () => {
  const launchOptions = {
    headless: true,
    executablePath: '/usr/bin/google-chrome',
    args: [
      '--disable-blink-features=AutomationControlled',
      '--no-first-run',
      '--no-default-browser-check',
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage',
      '--window-size=1920,1080',
      '--disable-infobars',
      '--window-position=0,0',
      '--ignore-certificates-errors',
      '--ignore-ssl-errors',
      '--lang=zh-CN',
      '--disable-features=TranslateUI',
      '--disable-notifications',
      '--mute-audio',
      '--use-fake-ui-for-media-stream',
      '--use-fake-device-for-media-stream',
    ],
    defaultViewport: { width: 1920, height: 1080, deviceScaleFactor: 1 },
    ignoreDefaultArgs: ['--enable-automation'],
    protocolTimeout: 120000,
  };

  const browser = await puppeteer.launch(launchOptions);
  const page = await browser.newPage();

  const uniqueBooks = [{
    id: 1439,
    url: 'https://www.zhiyuanzhongyi.com/introduces?id=1439'
  }];

  for (const book of uniqueBooks) {
    const { id, url } = book;
    const bookDir = path.join(outputDir, book.name);
    if (!fs.existsSync(bookDir)) fs.mkdirSync(bookDir, { recursive: true });

    // ✅ 增量：跳过已爬取的书
    if (fs.existsSync(path.join(bookDir, '000_目录.md')) && booksProgress[book.name]) {
      console.log(`⏭️ 跳过已爬取书籍: ${book.name}`);
      continue;
    }

    console.log(`🔍 正在爬取书籍: ${book.name} (ID=${id})`);

    try {
      await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 });
    } catch (e) {
      await sleep(3200);
      try {
        await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 });
      } catch (e) {
        await sleep(8200);
        await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 60000 });
      }
    }
    await sleep(2000);

    // ✅ 获取书籍结构数据（支持“无标题卷”使用章节名作为卷名）
    const bookData = await page.evaluate((bookId) => {
      const bookTitle = document.querySelector('.book_title')?.innerText.trim() || '未知书名';
      const volumeElements = document.querySelectorAll('.catalog_item');
      const volumes = [];

      volumeElements.forEach((volEl, index) => {
        const volumeTitleEl = volEl.querySelector('.catalog_title');
        const volumeTitle = volumeTitleEl ? volumeTitleEl.innerText.trim() : '';
        const chapterEls = volEl.querySelectorAll('.catalog_chapter a');

        const chapters = [];
        chapterEls.forEach(a => {
          const chapterName = a.querySelector('.chapter_name')?.innerText.trim() || '未知章节';
          const href = a.getAttribute('href');
          const fullUrl = new URL(href, location.origin).href;
          chapters.push({ chapterName, url: fullUrl });
        });

        if (volumeTitle) {
          // 有标题 → 正常卷
          volumes.push({
            type: 'normal',
            index,
            volumeTitle,
            chapters
          });
        } else if (chapters.length > 0) {
          // 无标题但有章节 → 视为独立章节卷
          volumes.push({
            type: 'chapter-as-volume',
            index,
            volumeTitle: chapters[0].chapterName, // 用第一个章节名作为卷名
            chapterUrl: chapters[0].url,
            chapters: []
          });
        } else {
          // 空卷（极少）
          volumes.push({
            type: 'empty',
            index,
            volumeTitle: `空卷-${index + 1}`,
            chapters: []
          });
        }
      });

      return { id: bookId, bookTitle, volumes };
    }, id);

    console.log(`✅ 获取《${bookData.bookTitle}》共 ${bookData.volumes.length} 卷`);

    // ✅ 生成 000_目录.md —— 清晰展示“卷+章节”结构，无“未命名卷”
    let docMd = `# ${bookData.bookTitle}\n\n`;
    docMd += `> 《[${baseSite}-${bookData.bookTitle}](${url}) 书籍 ID: ${bookData.id}\n\n`;

    for (const vol of bookData.volumes) {
      const safeVolumeTitle = vol.volumeTitle.replace(/[\\/:\*\?\"<>\|]/g, '_');
      const volumeFilename = `${String(vol.index + 1).padStart(3, '0')}_${safeVolumeTitle}.md`;

      docMd += `## ${vol.volumeTitle}\n\n`;
      docMd += `- [📖 ${vol.volumeTitle}](${volumeFilename})\n\n`;

      if (vol.type === 'normal' && vol.chapters.length > 0) {
        docMd += `  > 本卷包含 ${vol.chapters.length} 个章节：\n`;
        vol.chapters.forEach(ch => {
          docMd += `  - ${ch.chapterName}\n`;
        });
        docMd += '\n';
      } else if (vol.type === 'chapter-as-volume') {
        docMd += `  > （本卷对应独立章节，已合并内容）\n\n`;
      } else {
        docMd += `  > （本卷暂无内容）\n\n`;
      }
    }

    const docPath = path.join(bookDir, '000_目录.md');
    fs.writeFileSync(docPath, docMd, 'utf8');
    console.log(`📄 目录已保存: ${docPath}`);

    // ✅ 遍历每一卷并生成文件
    for (let i = 0; i < bookData.volumes.length; i++) {
      const vol = bookData.volumes[i];
      const safeVolumeTitle = vol.volumeTitle.replace(/[\\/:\*\?\"<>\|]/g, '_');
      const volumeFilename = `${String(i + 1).padStart(3, '0')}_${safeVolumeTitle}.md`;
      const volumeFilePath = path.join(bookDir, volumeFilename);
      // ✅ 断点续爬判断：文件存在 且 内容不含“爬取失败” 才视为已完成
      if (fs.existsSync(volumeFilePath)) {
        const content = fs.readFileSync(volumeFilePath, 'utf8');
        if (!content.includes('爬取失败：')) {
          console.log(`⏭️ 跳过已完整爬取的卷: ${volumeFilename}`);
          continue;
        } else {
          console.log(`🔄 重新爬取部分失败的卷: ${volumeFilename}`);
        }
      }
      console.log(`⏭️ 开始处理卷: ${String(i + 1).padStart(3, '0')}_${safeVolumeTitle} / ${bookData.volumes.length}卷-${bookData.bookTitle}`);
      let volumeMd = `# ${bookData.bookTitle} · ${vol.volumeTitle}\n\n`;

      if (vol.type === 'normal') {
        // 正常卷：合并所有章节
        for (const ch of vol.chapters) {
          try {
            console.log(`    📖 正在爬取章节: ${ch.chapterName}`);
            try {
              await page.goto(ch.url, { waitUntil: 'domcontentloaded', timeout: 60000 });
            } catch (e) {
              await sleep(1000);
              if (e.message.includes('timeout of')) {
                await sleep(randomMs(10000, 30000));
              }
              await page.goto(ch.url, { waitUntil: 'domcontentloaded', timeout: 60000 });
            }
            await sleep(1000);

            const contentHtml = await page.evaluate(() => {
              const el = document.querySelector('.chapter_content');
              return el ? el.innerHTML : null;
            });

            if (!contentHtml) {
              volumeMd += `### ${ch.chapterName}\n\n> 内容未获取到。\n\n`;
              continue;
            }

            let markdown = turndownService.turndown(contentHtml);
            markdown = markdown.replace(/\n{3,}/g, '\n\n');

            volumeMd += `### ${ch.chapterName}\n\n`;
            volumeMd += markdown + '\n\n';
            console.log(`    ✅ 已添加: ${ch.chapterName}`);
          } catch (err) {
            console.error(`    ❌ 爬取章节失败: ${ch.chapterName}`, err.message);
            volumeMd += `### ${ch.chapterName}\n\n> 爬取失败：${err.message}\n\n`;
          }
        }
      } else if (vol.type === 'chapter-as-volume') {
        // 独立章节卷：直接爬取该章节
        try {
          console.log(`    📖 正在爬取独立章节: ${vol.volumeTitle}`);
          try {
            await page.goto(vol.chapterUrl, { waitUntil: 'domcontentloaded', timeout: 60000 });
          } catch (e) {
            await sleep(1000);
            await page.goto(vol.chapterUrl, { waitUntil: 'domcontentloaded', timeout: 60000 });
          }
          await sleep(1000);

          const contentHtml = await page.evaluate(() => {
            const el = document.querySelector('.chapter_content');
            return el ? el.innerHTML : null;
          });

          if (!contentHtml) {
            volumeMd += `> 内容未获取到。\n\n`;
          } else {
            let markdown = turndownService.turndown(contentHtml);
            markdown = markdown.replace(/\n{3,}/g, '\n\n');
            volumeMd += markdown + '\n\n';
          }
          console.log(`    ✅ 已添加: ${vol.volumeTitle}`);
        } catch (err) {
          console.error(`    ❌ 爬取独立章节失败: ${vol.volumeTitle}`, err.message);
          volumeMd += `> 爬取失败：${err.message}\n\n`;
        }
      } else {
        volumeMd += `> 本卷为空，无内容。\n\n`;
      }

      fs.writeFileSync(volumeFilePath, volumeMd, 'utf8');
      console.log(`    💾 已保存卷文件: ${volumeFilename}`);
    }

    await sleep(500);
  }

  await browser.close();
  console.log('🎉 所有书籍按卷合并完成！');
})();